/*
* Copyright 2014-2015 GameUp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gameup.android.http;
import com.google.gson.JsonParseException;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.io.Reader;
import java.net.HttpURLConnection;
import io.gameup.android.GameUpException;
import io.gameup.android.util.Utils;
import io.gameup.android.json.GsonFactory;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
/**
* Handles sending requests and parsing responses.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RequestSender {
/**
* @param request A request that isn't expected to return a response body,
* or where the response body should be ignored.
*/
public static void send(final @NonNull Request request) {
try {
final Response response =
OkHttpClientFactory.getClient().newCall(request).execute();
if (response.code() != HttpURLConnection.HTTP_OK &&
response.code() != HttpURLConnection.HTTP_NO_CONTENT &&
response.code() != HttpURLConnection.HTTP_CREATED) {
Reader in = null;
try {
in = response.body().charStream();
// If the response input stream is null, assume no connection.
if (in == null) {
throw new GameUpException(0, "No Connection");
}
try {
throw GsonFactory.get().fromJson(in, GameUpException.class);
}
catch (final JsonParseException e) {
throw new GameUpException(500, "Unknown error data", null);
}
}
finally {
Utils.closeQuietly(in);
}
}
}
catch(final IOException e) {
throw new GameUpException(400, "Failed to execute request", null);
}
}
/**
* @param request A request.
* @param responseType The class type to try to deserialise the body to.
* @return An instance of the given type, or null if the response body was
* empty.
*/
public static <T> T send(final @NonNull Request request,
final @NonNull Class<T> responseType) {
Reader in = null;
try {
final Response response =
OkHttpClientFactory.getClient().newCall(request).execute();
in = response.body().charStream();
if (response.code() != HttpURLConnection.HTTP_OK &&
response.code() != HttpURLConnection.HTTP_NO_CONTENT &&
response.code() != HttpURLConnection.HTTP_CREATED) {
// If the response input stream is null, assume no connection.
if (in == null) {
throw new GameUpException(0, "No Connection");
}
try {
throw GsonFactory.get().fromJson(in, GameUpException.class);
} catch (final JsonParseException e) {
throw new GameUpException(500, "Unknown error data", null);
}
}
try {
return GsonFactory.get().fromJson(in, responseType);
}
catch (final JsonParseException e) {
throw new GameUpException(400,
"Response data does not match expected entity", null);
}
}
catch(final IOException e) {
throw new GameUpException(400, "Failed to execute request", null);
}
finally {
Utils.closeQuietly(in);
}
}
}